package com.study.utils.mybatisplus;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.study.enums.HttpStatusEnum;
import com.study.model.dto.StudentQueryDTO;
import com.study.model.entity.StudentDO;
import com.study.model.vo.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class PStudentServiceImpl implements PStudentService {


    @Resource
    private PStudentMapper pStudentMapper;

    @Override
    public StudentDO selectStudentById(Long id) {
        StudentDO studentDO = pStudentMapper.selectById(id);
        if(studentDO == null) {
            throw new ErrorCodeException(HttpStatusEnum.RESOURCE_NOT_FOUND.getCode(), "Student does not exist. id: " + id);
        }
        return BeanUtil.copyProperties(studentDO, StudentDO.class);
    }

    @Override
    public PageVO<StudentDO> selectStudentList(StudentQueryDTO studentQueryDTO) {
        Page<StudentDO> page;
        if (studentQueryDTO.getPageNum()==null || studentQueryDTO.getPageSize()==null) {
            page = PageHelper.startPage(1, 10);
        } else {
            page = PageHelper.startPage(studentQueryDTO.getPageNum(), studentQueryDTO.getPageSize());
        }

        LambdaQueryWrapper<StudentDO> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(studentQueryDTO.getName())) {
            wrapper.like(StudentDO::getName, studentQueryDTO.getName());
        }
        if (studentQueryDTO.getAge() != null) {
            wrapper.eq(StudentDO::getAge, studentQueryDTO.getAge());
        }
        wrapper.orderByDesc(StudentDO::getCreateTime);
        List<StudentDO> studentDOList = pStudentMapper.selectList(wrapper);

        // 转换
        List<StudentDO> list = studentDOList.stream().map(item -> {
            StudentDO studentDO = new StudentDO();
            BeanUtils.copyProperties(item, studentDO);
            return studentDO;
        }).collect(Collectors.toList());

        // 返回封装后的分页信息
        return PageVO.getPageVO(page, list);
    }

    @Override
    public int insertStudent(StudentDO studentDO) {
        if(ObjectUtils.isEmpty(studentDO)) {
            throw new ErrorCodeException(HttpStatusEnum.INVALID_PARAMETER.getCode(), "新增学生对象名字不能为空");
        }
        // 判断是否有重复的名字
        LambdaQueryWrapper<StudentDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StudentDO::getName, studentDO.getName());
        StudentDO tempItem = pStudentMapper.selectOne(wrapper);
        if(tempItem != null) {
            throw new ErrorCodeException(HttpStatusEnum.ERROR.getCode(), "学生名字重复");
        }
        studentDO.setCreateTime(new Date());
        studentDO.setUpdateTime(new Date());
        int insertNum = pStudentMapper.insert(studentDO);
        if(insertNum <= 0) {
            throw new ErrorCodeException(HttpStatusEnum.NO_ROW_AFFECTED.getCode(), HttpStatusEnum.NO_ROW_AFFECTED.getMsg());
        }
        return insertNum;
    }

    @Override
    public int updateStudent(StudentDO studentDO) {
        if(studentDO.getId() == null) {
            throw new ErrorCodeException(HttpStatusEnum.INVALID_PARAMETER.getCode(), "ID不能为空");
        }
        studentDO.setUpdateTime(new Date());
        int updateNum = pStudentMapper.updateById(studentDO);
        if(updateNum <= 0) {
            throw new ErrorCodeException(HttpStatusEnum.NO_ROW_AFFECTED.getCode(), HttpStatusEnum.NO_ROW_AFFECTED.getMsg());
        }
        return updateNum;
    }

    @Override
    public int deleteStudentById(Long id) {
        StudentDO studentDO = pStudentMapper.selectById(id);
        if(studentDO == null) {
            throw new ErrorCodeException(HttpStatusEnum.RESOURCE_NOT_FOUND.getCode(), "Student does not exist. ");
        }
        int deleteNum = pStudentMapper.deleteById(id);
        if(deleteNum <= 0) {
            throw new ErrorCodeException(HttpStatusEnum.NO_ROW_AFFECTED.getCode(), HttpStatusEnum.NO_ROW_AFFECTED.getMsg());
        }
        return deleteNum;
    }

    @Override
    public int deleteStudentByIds(Long[] ids) {
        if(ids == null || ids.length == 0) {
            throw new ErrorCodeException(HttpStatusEnum.INVALID_PARAMETER.getCode(), "ids不能为空");
        }
        List<StudentDO> studentDOList = pStudentMapper.selectBatchIds(Arrays.asList(ids));
        if(studentDOList.size() == 0) {
            throw new ErrorCodeException(HttpStatusEnum.RESOURCE_NOT_FOUND.getCode(), HttpStatusEnum.RESOURCE_NOT_FOUND.getMsg());
        }
        int deleteNum = pStudentMapper.deleteBatchIds(Arrays.asList(ids));
        if(deleteNum <= 0) {
            throw new ErrorCodeException(HttpStatusEnum.NO_ROW_AFFECTED.getCode(), HttpStatusEnum.NO_ROW_AFFECTED.getMsg());
        }
        return deleteNum;
    }
}
